﻿<?php
include_once('SEGMysql.php');

class SEGDb { 
	public $banco;

	public function SEGDb ($host=false, $db=false, $user=false, $senha=false,$nmdb='mysql') {
		if (!$host)
			$host = '127.0.0.1';
		if (!$db) {
			$db = DBCONTROLE;
			if (isset($_SESSION['usu_db']))
				$db = $_SESSION['usu_db']; 
		}

		if (strtoupper($nmdb) == 'MYSQL')
			$this -> banco = new SEGMysql($host,$db,$user,$senha);
	}
	
	public function SEGMontaSQLGrid ($tabela = false, $joins = '', $campos = '*', $pwhere = '') {	
		if ($tabela) {			
			//=== Dados para a GRId ===
			if ($_GET['pagenum'] >= 0) {
				$pagenum  = $_GET['pagenum'];
				$pagesize = $_GET['pagesize'];
				$start    = $pagenum * $pagesize;
				$query    = "SELECT SQL_CALC_FOUND_ROWS ".$campos." ".
							"FROM ".$tabela." ".
							$joins." ".
							(($pwhere <> '') ? ' where '.$pwhere:'').
							"LIMIT $start, $pagesize";
				$this->banco->query($query);
				$sql      = "SELECT FOUND_ROWS() AS `found_rows`;";
				$this->banco->query($sql);	
				$total_rows = $this->banco->result['found_rows'];
				$filterquery = "";				
				
				// filter data.
				if (isset($_GET['filterscount']))
				{
					$filterscount = $_GET['filterscount'];
					
					if ($filterscount > 0)
					{
						$where = " WHERE (";
						$tmpdatafield = "";
						$tmpfilteroperator = "";
						for ($i=0; $i < $filterscount; $i++)
						{
							// get the filter's value.
							$filtervalue = $_GET["filtervalue" . $i];
							// get the filter's condition.
							$filtercondition = $_GET["filtercondition" . $i];
							// get the filter's column.
							$filterdatafield = $_GET["filterdatafield" . $i];
							// get the filter's operator.
							$filteroperator = $_GET["filteroperator" . $i];
							
							if ($tmpdatafield == "")
							{
								$tmpdatafield = $filterdatafield;			
							}
							else if ($tmpdatafield <> $filterdatafield)
							{
								$where .= ")AND(";
							}
							else if ($tmpdatafield == $filterdatafield)
							{
								if ($tmpfilteroperator == 0)
								{
									$where .= " AND ";
								}
								else $where .= " OR ";	
							}
							
							// build the "WHERE" clause depending on the filter's condition, value and datafield.
							switch($filtercondition)
							{
								case "NOT_EMPTY":
								case "NOT_NULL":
									$where .= " " . $filterdatafield . " NOT LIKE '" . "" ."'";
									break;
								case "EMPTY":
								case "NULL":
									$where .= " " . $filterdatafield . " LIKE '" . "" ."'";
									break;
								case "CONTAINS_CASE_SENSITIVE":
									$where .= " BINARY  " . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
									break;
								case "CONTAINS":
									$where .= " " . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
									break;
								case "DOES_NOT_CONTAIN_CASE_SENSITIVE":
									$where .= " BINARY " . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
									break;
								case "DOES_NOT_CONTAIN":
									$where .= " " . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
									break;
								case "EQUAL_CASE_SENSITIVE":
									$where .= " BINARY " . $filterdatafield . " = '" . $filtervalue ."'";
									break;
								case "EQUAL":
									$where .= " " . $filterdatafield . " = '" . $filtervalue ."'";
									break;
								case "NOT_EQUAL_CASE_SENSITIVE":
									$where .= " BINARY " . $filterdatafield . " <> '" . $filtervalue ."'";
									break;
								case "NOT_EQUAL":
									$where .= " " . $filterdatafield . " <> '" . $filtervalue ."'";
									break;
								case "GREATER_THAN":
									$where .= " " . $filterdatafield . " > '" . $filtervalue ."'";
									break;
								case "LESS_THAN":
									$where .= " " . $filterdatafield . " < '" . $filtervalue ."'";
									break;
								case "GREATER_THAN_OR_EQUAL":
									$where .= " " . $filterdatafield . " >= '" . $filtervalue ."'";
									break;
								case "LESS_THAN_OR_EQUAL":
									$where .= " " . $filterdatafield . " <= '" . $filtervalue ."'";
									break;
								case "STARTS_WITH_CASE_SENSITIVE":
									$where .= " BINARY " . $filterdatafield . " LIKE '" . $filtervalue ."%'";
									break;
								case "STARTS_WITH":
									$where .= " " . $filterdatafield . " LIKE '" . $filtervalue ."%'";
									break;
								case "ENDS_WITH_CASE_SENSITIVE":
									$where .= " BINARY " . $filterdatafield . " LIKE '%" . $filtervalue ."'";
									break;
								case "ENDS_WITH":
									$where .= " " . $filterdatafield . " LIKE '%" . $filtervalue ."'";
									break;
							}
											
							if ($i == $filterscount - 1)
							{
								$where .= ")". (($pwhere <> '') ? ' and '.$pwhere : '');
							}
							
							$tmpfilteroperator = $filteroperator;
							$tmpdatafield = $filterdatafield;			
						}
						// build the query.
						$query = "SELECT * FROM ".$tabela." ".$where;
						$filterquery = $query;
						$this->banco->query($query);
						$sql = "SELECT FOUND_ROWS() AS `found_rows`;";
						$this->banco->query($sql);
						$new_total_rows = $this->result['found_rows'];		
						$query = "SELECT * FROM ".$tabela." ".$where." LIMIT $start, $pagesize";		
						$total_rows = $new_total_rows;	
					}
				}
				
				if (isset($_GET['sortdatafield']))
				{
				
					$sortfield = $_GET['sortdatafield'];
					$sortorder = $_GET['sortorder'];
					
					if ($sortorder != '')
					{
						if ($_GET['filterscount'] == 0)
						{
							if ($sortorder == "desc")
							{
								$query = "SELECT * FROM ".$tabela.(($pwhere <> '') ? ' where '.$pwhere:'')." ORDER BY" . " " . $sortfield . " DESC LIMIT $start, $pagesize";
							}
							else if ($sortorder == "asc")
							{
								$query = "SELECT * FROM ".$tabela.(($pwhere <> '') ? ' where '.$pwhere:'')." ORDER BY" . " " . $sortfield . " ASC LIMIT $start, $pagesize";
							}
						}
						else
						{
							if ($sortorder == "desc")
							{
								$filterquery .= " ORDER BY" . " " . $sortfield . " DESC LIMIT $start, $pagesize";
							}
							else if ($sortorder == "asc")	
							{
								$filterquery .= " ORDER BY" . " " . $sortfield . " ASC LIMIT $start, $pagesize";
							}
							$query = $filterquery;
						}		
					}
				}
				
				return $query;				
			}
		}
	}
} 
?>